public class Singleton {
    // 私有静态实例变量，初始化为null
    private static Singleton instance = null;

    // 私有构造函数，防止外部实例化
    private Singleton() {
        System.out.println("Singleton instance created.");
    }

    // 公共静态方法获取单例实例
    public static Singleton getInstance() {
        // 懒汉式单例模式，首次调用时创建实例
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

    // 示例方法，展示单例功能
    public void showMessage() {
        System.out.println("Hello from the Singleton!");
    }


    public static void main(String[] args) {
        // 获取单例实例并调用方法
        Singleton singleton1 = Singleton.getInstance();
        singleton1.showMessage();


        Singleton singleton2 = Singleton.getInstance();
        singleton2.showMessage();


        if (singleton1 == singleton2) {
            System.out.println("singleton1 and singleton2 are the same instance.");
        } else {
            System.out.println("singleton1 and singleton2 are different instances.");
        }
    }   
}


